package ex.recruit.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.FirstTrial;
import modules.recruit.entity.Visit;
import modules.recruit.form.VisitForm;
import modules.recruit.service.VisitService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.PropertyFilterForSql;
import com.guinea.core.orm.Page;
import com.guinea.core.orm.PropertyFilter;
import com.guinea.core.service.EntityService;
import com.guinea.core.utils.ServletUtils;

public class VisitActionEx extends CommonApplyJudgment {
	
	
	VisitService visitService;

	public EntityService getEntityService() {
		return visitService;
	}

	public VisitService getVisitService() {
		return visitService;
	}

	public void setVisitService(VisitService visitService) {
		this.visitService = visitService;
	}


	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form, request);
		if (entity == null && applyBase != null) {
			entity = applyBase.getVisit();
		}
	}
	
	/**
	 * 列出所有对象的Action函数.
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 从request中获取含属性前缀名的参数,构造去除前缀名后的参数Map.
		Map filterParamMap = ServletUtils.getParametersStartingWith(request,
				FILTER_PREFIX + "_");
		List filters = PropertyFilterForSql.buildFromParaMap(filterParamMap);
		Page page = PropertyFilter.buildPageFromHttpRequest(request,
				REQUEST_PAGE);
		
		filters.add(new PropertyFilterForSql("step", "4"));
		page = getApplyBaseServiceEx().getApplyProcess(page, filters, true);

		request.setAttribute(REQUEST_PAGE, page);
		request.setAttribute(FILTER_MAP, filterParamMap);
		return mapping.findForward(LIST);
	}
	
	/**
	 * 显示修改对象Form的Action函数.
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		bindForm(form, entity);
		VisitForm visitForm = (VisitForm)form;
		visitForm.setApplyBaseId(applyBaseId);
		FirstTrial firstTrial = applyBase.getFirstTrial();
		if(firstTrial != null) request.setAttribute("planVisitDate",firstTrial.getPlanVisitDate());
		
		return handleEdit(mapping,form,request,response);
	}
	
	protected void doInitEntity(HttpServletRequest request, ActionForm form) {
		super.doInitEntity(request, form);
		if (isCreateOperation) {
			Visit visit = (Visit) entity;
			visit.setBase(applyBase);
			entity = visit;
		}
	}

}
